/**
 * 
 */

OrderDetailPage = {
		'BindEvent': function(){
			$('#updateOrderInvoiceTitle').bind('click', function(){
				var thirdOrderId = $('#order_id').val();
				
				var invoiceTitle = $('#invoiceTitle').val();
				
				if(invoiceTitle==null || invoiceTitle=='' || invoiceTitle ==undefined){
					alert("修改发票抬头不能为空");
					return;
				}
				
				var result = OrderService.UpdateInvoiceTitle(thirdOrderId,
						invoiceTitle);
				
				if(result.IsSuccess != 0){
					alert("完成修改，请刷新");
				}
				
			});
			//红票
			$('#redInvoice').bind('click', function(){
				
				var orderId = $('#order_id').val();
				var redReason = $('#redReason').val();
				
				if(redReason==''){
					alert('请选择冲红原因');
					return false;
				}
				var result = InvoiceService.redInvoice(orderId,
						redReason);
					alert('冲红申请成功'); 
					window.location.reload();
				
			});
		//纸质发票普通
		$('#paperInvoice').bind('click', function(){
			
            var invoice_status_check = $('#invoice_status_check').val();
            
            var cashBackStatus = $('#cash_back_status').val();

			
			if(!(invoice_status_check=='1'||invoice_status_check=='14')){
				alert("发票状态不是未开票状态");
				return false;
			}
			
			if(cashBackStatus!=1){
				alert("未导入返现金额！！！");
				return false;
			}
			
            var invoice_kind_check = $('#invoice_kind').val();

			if(invoice_kind_check=='0'){
				alert("非商品订单不开发票");
				return false;
			}
			
			var chk_value =[]; 
			chk_value.push($('#order_id').val()); 
				var result = InvoiceService.ApplyInvoice(chk_value,22);
				if(result.IsSuccess == 1){
					alert(result.Result);
					window.location.reload();
				}
		});
		//纸质发票专票
		$('#specialPaperInvoice').bind('click', function(){
			
            var invoice_status_check = $('#invoice_status_check').val();
            
            var cashBackStatus = $('#cash_back_status').val();

			
			if(!(invoice_status_check=='1'||invoice_status_check=='14')){
				alert("发票状态不是未开票状态");
				return false;
			}
			
			if(cashBackStatus!=1){
				alert("未导入返现金额！！！");
				return false;
			}
			
            var invoice_kind_check = $('#invoice_kind').val();

			if(invoice_kind_check=='0'){
				alert("非商品订单不开发票");
				return false;
			}
			
			var chk_value =[]; 
			chk_value.push($('#order_id').val()); 
				var result = InvoiceService.ApplyInvoice(chk_value,23);
				if(result.IsSuccess == 1){
					alert(result.Result);
					window.location.reload();
				}
		});
		//电子发票
		$('#electronicInvoice').bind('click', function(){
			var chk_value =[]; 
			var invoiceTitle = $('#invoiceTitle').val();
			
            var cashBackStatus = $('#cash_back_status').val();

			var error_msg = "";
			var invoice_status_check = $('#invoice_status_check').val();
			
            var invoice_kind_check = $('#invoice_kind').val();
            
            var order_date = $('#order_date').val();
            
            var tm_date_string = "2016-05-09 10:30:00";
            tm_date_string =  tm_date_string.replace(/-/g,"/");
            var tm_date = new Date(tm_date_string);
        
            
            var  order_date_string=order_date.toString();
            order_date_string =  order_date_string.replace(/-/g,"/");
            
            var order_date = new Date(order_date_string);
            var third = $('#thirdOrderId').val();
            
            if(tm_date >=order_date ){
            	alert("2016.05.09 上午10点30分之前的单子只能开纸质或手工开票");
				return false;
              }

			if(invoice_kind_check=='0'){
				alert("非商品或者退货单不开发票");
				return false;
			}
			
			if(invoice_kind_check=='22'){
				alert("只能开纸质发票");
				return false;
			}
			
			if(!(invoice_status_check=='1'||invoice_status_check=='14')){
				alert("发票状态不是未开票状态");
				return false;
			}
			
			if(cashBackStatus!=1){
				alert("未导入返现金额！！！");
				return false;
			}

			if(error_msg != ""){
				alert(error_msg);
				return false;
			}
			
			chk_value.push($('#order_id').val()); 
				var result = InvoiceService.ApplyInvoice(chk_value,21);
				if(result.IsSuccess == 1){
					alert(result.Result);
					window.location.reload();
				}
			
		})
		
	}	
		
};
 



$(document).ready(function(){
	OrderDetailPage.BindEvent();
});

